package model

import "time"

type Account struct {
	Id int64 `xorm:"pk autoincr"`
	DepartMentId int64
	AccountName string
	Account string
	Level int
	Incumbency int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type DepartMent struct {
	Id int64 `xorm:"pk autoincr"`
	DepartMentName string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Order struct {
	Id int64 `xorm:"pk autoincr"`
	OrderType int
	OrderState int
	CustomerId int64
	CustomerName string
	NextSinglePersonId int64
	NextSinglePersonName string
	Meal string
	TotalPrice int64
	Region string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Customer struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Type int
	FounderId int
	FounderName string
	BelongerId int
	BelongerName string
	AllocationTime time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}
type ReviewAccount struct {
	AccountId int64
	Account string
	AccountName string
}

type Reviem struct {
	OrderAmount int64
	OrderSales int
	ReviewDepartMentList []*ReviewDepartMent
}

type ReviewDepartMent struct {
	DepartMentId int64
	DepartMentName string
	ReviewAccountList []*ReviewAccount
}

type ReviewDocuments struct {
	Id int64 `xorm:"pk autoincr"`
	State int
	DepartMentId int64
	DepartMentName string
	AccountId int64
	AccountName string
	Account string
	OrderAmount int64
	OrderSales int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ReviewInformation struct {
	Id int64 `xorm:"pk autoincr"`
	RoleId int64
	OrderId int64
	OrderType int
	State int
	CustomerId int64
	CustomerName string
	AccountId int64
	AccountName string
	TotalPrice int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

func InsertReview(r *Reviem)(map[string]interface{}, error){

	for _, rdm := range r.ReviewDepartMentList{
		for _, ra := range rdm.ReviewAccountList{
			var rd = ReviewDocuments{
				State:          0,
				DepartMentId:   rdm.DepartMentId,
				DepartMentName: rdm.DepartMentName,
				AccountId:      ra.AccountId,
				AccountName:    ra.AccountName,
				Account:        ra.Account,
				OrderAmount:    r.OrderAmount,
				OrderSales:     r.OrderSales,
				CreatedAt:      time.Now(),
				UpdatedAt:      time.Now(),
			}
			x.Insert(rd)
		}
	}

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}

func QueryReview()([]*ReviewDocuments, error){

	var rdList []*ReviewDocuments
	x.Find(&rdList)
	return rdList, nil
}

func JudgeReview(o *Order, oa int64, cId int64)(){

	var oList []*Order
	x.Where("customer_id = ?", cId).Find(&oList)

	var rd ReviewDocuments
	x.Where("account_id = ?", o.NextSinglePersonId).And("state = ?", 1).Get(&rd)

	if rd.Id != 0{
		if len(oList) >= rd.OrderSales && oa > rd.OrderAmount{
			var ri = ReviewInformation{
				RoleId:               3,
				OrderId:              o.Id,
				OrderType:            o.OrderType,
				State:                0,
				CustomerId:           o.CustomerId,
				CustomerName:         o.CustomerName,
				AccountId:   		  o.NextSinglePersonId,
				AccountName:          o.NextSinglePersonName,
				TotalPrice:           o.TotalPrice,
				CreatedAt:            time.Now(),
				UpdatedAt:            time.Now(),
			}
			x.Insert(&ri)
		}
	}
}




















































